#include "textmenu.h"
#include "delay.h"
#include "led.h"
#include "key.h"

const char *menuItems[MAX_MENU] = {
    "1.Blink",
    "2.LED OFF",
    "3.LED ON",
    "4.Brightness",
};

void (*pSelectFuncCB[MAX_MENU])(void) = {
    blink,
    ledoff,
    ledon,
    backLightBrightness,
};

unsigned char textMenu_KeyDownCheck(void)
{
    return Key_Scan(KEY1_GPIO_PORT, KEY1_GPIO_PIN);
}

unsigned char textMenu_KeyDownCallback(void)
{
    textMenu.curMenuID++;
    if (textMenu.curMenuID >= (MAX_MENU - 1)) {
        textMenu.curMenuID = (MAX_MENU - 1);
    }

    return 1;
}

unsigned char textMenu_KeyUpCheck(void)
{
    return 0;
}

unsigned char textMenu_KeyUpCallback(void)
{
    textMenu.curMenuID--;
    if (textMenu.curMenuID < 0) {
        textMenu.curMenuID = 0;
    }

    return 1;
}

unsigned char textMenu_KeySelectCheck(void)
{
    return 0;
}

unsigned char textMenu_KeySelectCallback(void)
{
    delay_ms(500);
    pSelectFuncCB[textMenu.curMenuID];
    return 1;
}



void blink()
{
    while (textMenu.KeySelectCheck()) {
        LED1_TOGGLE;
        delay_ms(500);
    }
    delay_ms(1000);
}

void ledoff(void)
{
    LED1_OFF;
    delay_ms(500);
}

void ledon(void)
{
    LED1_ON;
    delay_ms(500);
}

void backLightBrightness(void)
{
    unsigned char brightness = 100;
    unsigned char strOut[50] = {0};

    u8g2_ClearBuffer(&u8g2);
    while (textMenu.KeySelectCheck()) {
        if (textMenu.KeyDownCheck()) {
            brightness -= 5;
            delay_ms(200);
        }

        if (textMenu.KeyUpCheck()) {
            brightness += 5;
            delay_ms(200);
        }

        sprintf(strOut, "Brightness: %d", brightness);
        u8g2_DrawStr(5, 25, strOut);
        u8g2_SendBuffer(&u8g2);
        u8g2_ClearBuffer(&u8g2);
    }
    delay_ms(1000);
}



void textMenu_Init(pTextMenu_t pTextMenu)
{
    pTextMenu->pTmenuList        = menuItems;
    pTextMenu->textMenuSize      = sizeof(menuItems);
    pTextMenu->curMenuID         = 0;
    pTextMenu->KeyDownCheck      = textMenu_KeyDownCheck;
    pTextMenu->KeyDownCallback   = textMenu_KeyDownCallback;
    pTextMenu->KeyUpCheck        = textMenu_KeyUpCheck;
    pTextMenu->KeyUpCallback     = textMenu_KeyUpCallback;
    pTextMenu->KeySelectCheck    = textMenu_KeySelectCheck;
    pTextMenu->KeySelectCallback = pSelectFuncCB;
}


void textMenu_Handle(pTextMenu_t pTextMenu)
{
    if (pTextMenu->KeyDownCheck()) {
        pTextMenu->KeyDownCallback();
    }

    if (pTextMenu->KeyUpCheck()) {
        pTextMenu->KeyUpCallback();
    }

    if (pTextMenu->KeySelectCheck()) {
        pTextMenu->KeySelectCallback();
    }

    for (byte m = 0; m <= menuLength; m++) {
        if (menuPosition < menuInLCD) {
            if (menuPosition == m) {
                lcd.drawRFrame(0, m * textHeight, lcdWidth, boxHeight, 2);
            }
            lcd.setCursor(2, m * textHeight);
            lcd.println( menuItems[m - 1]);
        } else {
            if (menuPosition == m) {
                lcd.drawRFrame(0, (m - (1 + menuPosition - menuInLCD)) * textHeight, lcdWidth, boxHeight, 2);
            }
            lcd.setCursor(2, (m - (1 + menuPosition - menuInLCD)) * textHeight);
            lcd.println( menuItems[m - 1]);
        }
    }

    lcd.sendBuffer();
    lcd.clearBuffer();



}

